import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes material mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  /**
   * 主页
   */
  {
    path: '/',
    component: Layout,
    redirect: '/main',
    children: [{
      path: 'main',
      name: 'main',
      component: () => import('@/views/main/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  },
  /**
   * 作文
   */
  {
    path: '/',
    component: Layout,
    redirect: '/article',
    children: [
      {
        path: 'article',
        name: 'article',
        component: () => import('@/views/article/index'),
        meta: { title: '作文', icon: 'form' }
      }
    ]
  },
  /**
   * 作文查看
   */
  {
    path: '/article',
    component: Layout,
    children: [
      {
        path: '/article/visit',
        name: 'visit',
        component: () => import('@/views/article/visit/index')
      }
    ]
  },
  /**
   * 作文批阅
   */
  {
    path: '/article/visit',
    component: Layout,
    children: [
      {
        path: '/article/visit/review',
        name: 'review',
        component: () => import('@/views/article/review/index')
      }
    ]
  },
  /**
   * 阅读
   */
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: 'reading',
        name: 'reading',
        component: () => import('@/views/reading/index'),
        meta: { title: '阅读', icon: 'el-icon-reading' }
      }
    ]
  },
  /**
   * 资料
   */
  {
    path: '/material',
    component: Layout,
    redirect: '/material/menu1',
    name: 'material',
    meta: { title: '资料', icon: 'el-icon-document' },
    children: [
      {
        path: 'compositionlibrary',
        component: () => import('@/views/material/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: '作文库' }
      },
      {
        path: 'readinglibrary',
        component: () => import('@/views/material/menu2/index'),
        name: 'Menu2',
        meta: { title: '阅读库' }
      },
      {
        path: 'readingquestionbank',
        component: () => import('@/views/material/menu3/index'),
        name: 'Menu3',
        meta: { title: '阅读题库' }
      }
    ]
  },
  /**
   * 管理
   */
  {
    path: '/manage',
    component: Layout,
    redirect: '/manage/menu1',
    name: 'manage',
    meta: { title: '管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'classmanagement',
        component: () => import('@/views/manage/menu1/index'),
        name: 'Menu1',
        meta: { title: '班级管理' }
      },
      {
        path: 'downloadcenter',
        component: () => import('@/views/manage/menu2/index'),
        name: 'Menu2',
        meta: { title: '下载中心' }
      }
    ]
  },
  /**
   * 统计
   */
  {
    path: '/statistics',
    component: Layout,
    redirect: '/statistics/menu1',
    name: 'statistics',
    meta: { title: '统计', icon: 'el-icon-s-data' },
    children: [
      {
        path: 'compositionstatistics',
        component: () => import('@/views/statistics/menu1/index'),
        name: 'Menu1',
        meta: { title: '作文' }
      },
      {
        path: 'readingstatistics',
        component: () => import('@/views/statistics/menu2/index'),
        name: 'Menu2',
        meta: { title: '阅读' }
      }
    ]
  },
  /**
   * 设置
   */
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting/menu1',
    meta: { title: '设置', icon: 'el-icon-setting' },
    name: 'setting',
    children: [
      {
        path: 'datasetting',
        component: () => import('@/views/setting/menu1/index'),
        name: 'Menu1',
        meta: { title: '资料设置' }
      },
      {
        path: 'accountmessage',
        component: () => import('@/views/setting/menu2/index'),
        name: 'Menu2',
        meta: { title: '账户消息' }
      },
      {
        path: 'messagereminder',
        component: () => import('@/views/setting/menu3/index'),
        name: 'Menu2',
        meta: { title: '消息提醒' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
